package tech.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import tech.aistar.entity.Com;
import tech.aistar.entity.Post;
import tech.aistar.entity.UserInfo;
import tech.aistar.entity.massage.Massage;
import tech.aistar.service.IComService;
import tech.aistar.service.IMassageService;
import tech.aistar.service.IPostService;
import tech.aistar.service.IUserInfoService;
import tech.aistar.util.IdWorker;

import java.util.Date;

/**
 * Created by Administrator on 2019/10/9.
 */
@Controller
@RequestMapping("/send")
public class SendComAction {
    @Autowired
    private IComService comService;
    @Autowired
    private IMassageService massageService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IPostService postService;
    /**
     * 雪花id生成器
     */
    @Autowired
    private IdWorker idWorker;

    /**
     * 插入一级评论
     * 模拟用户id:1
     * @param postId
     * @param content
     * @return
     */
    @PostMapping("/sendComOne")
    public String sendComOne(Integer postId,String content,Integer userId){
//        System.out.println(content);
        //获取当前帖子的一级评论数
//        Integer postId1 = postId;
//        System.out.println(postId1+"ppppppppppppppppppppppppp");
        Long floorNum = comService.findOneComNum(1);
//        System.out.println(floorNum+"ffffffffffffffffff");
        Com com = new Com();
//        com.setComId(Math.toIntExact(idWorker.nextId()));
        //楼层数+1
        com.setFloor(floorNum.intValue()+1);
        com.setPostId(postId);
        Post post = postService.findPostByPostId(postId);
        com.setUserId(userId);
//        UserInfo reInfo = userInfoService.findById(post.getUserId());
//        UserInfo userInfo = userInfoService.findById(com.getUserId());
        com.setContent(content);
        com.setCtime(new Date());
        com.setPointNum(12);
        com.setFlag(1);
        comService.saveComment(com);
        System.err.println(com);
//        return "redirect:/com/comment?postId="+1;
//        Massage massage = new Massage(null,userId,userInfo.getuName(),null,post.getUserId(),reInfo.getuName(),content,null,postId,null,null,null);
//        massageService.addPostMassage();
        return "redirect:/com/comment?postId="+postId;
    }

    /**
     * 插入二级评论
     * 模拟用户id:2
     * @param postId
     * @param content
     * @return
     */
    @RequestMapping("/sendComTwo")
    public String sendComTwo(Integer postId,String content,Integer userId,Integer cid,Integer userid){

        Com com = new Com();
//        com.setComId(Math.toIntExact(idWorker.nextId()));
        com.setFloor(cid);
        com.setPostId(postId);
        com.setUserId(userid);
        com.setToUserId(userId);
        com.setContent(content);
        com.setCtime(new Date());
        com.setPointNum(35646);
        com.setFlag(0);
//        UserInfo userInfo = userInfoService.findById(com.getUserId());
//        UserInfo reInfo = userInfoService.findById(userId);
        comService.saveComment(com);
//        Massage massage = new Massage(null,userInfo.getId(),userInfo.getuName(),null,null,reInfo.getuName(),content,com.getComId(),null,null,null,null);
//        massageService.addComMassage(massage);
//        return "redirect:/com/comment?postId="+1;
        return "redirect:/com/comment?postId="+postId;
    }

    /**
     * 插入三级评论
     * 模拟用户id:3
     * @param postId
     * @param content
     * @return
     */
    @RequestMapping("/sendComThree")
    public String sendComThree(Integer postId,String content,Integer userId,Integer floor,Integer uId){

        Com com = new Com();
//        com.setComId(Math.toIntExact(idWorker.nextId()));
        com.setFloor(floor);
        com.setPostId(postId);
        com.setUserId(uId);
        com.setToUserId(userId);
        com.setContent(content);
        com.setCtime(new Date());
        com.setPointNum(35646);
        com.setFlag(0);
        comService.saveComment(com);
//        UserInfo userInfo = userInfoService.findById(com.getUserId());
//        UserInfo reInfo = userInfoService.findById(userId);
//        Massage massage = new Massage(null,userInfo.getId(),userInfo.getuName(),null,null,reInfo.getuName(),content,com.getComId(),null,null,null,null);
//        massageService.addComMassage(massage);
//        return "redirect:/com/comment?postId="+1;
        return "redirect:/com/comment?postId="+postId;
    }
}
